package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

public class FamilyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
			try {
				if(method.equals("list")){
					list(request,response);
				}else if(method.equals("add")){
					add(request,response);
				}else if(method.equals("del")){
					del(request,response);
				}else if(method.equals("edit")){
					edit(request,response);
				}else if(method.equals("getchild")){
					getchild(request,response);
				}else if(method.equals("showperson")){
					showperson(request,response);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	private void getchild(HttpServletRequest request,
			HttpServletResponse response) throws SQLException, IOException {
		// TODO Auto-generated method stub
		String areapcode = request.getParameter("areapcode");
		areapcode = new String(areapcode.getBytes("ISO-8859-1"),"UTF-8");//get传值 防中文乱码
		Area a= new Area();
		String resp ="";
		resp = JSON.toJSONString(a.getChildren(areapcode));
		System.out.println(areapcode+"   "+resp);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(resp);
	}


	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Family f = new Family();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = f.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/family/family_list.jsp";
		Family f = new Family();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = f.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void showperson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		String path = "/page/family/family_person.jsp";
		Person p = new Person();
		String page = request.getParameter("page");
		String familycode = request.getParameter("familycode");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = p.queryByFamilycode(familycode,currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}


	private void add(HttpServletRequest request, HttpServletResponse response) throws SQLException, ParseException, ServletException, IOException {
		// TODO Auto-generated method stub
		String path = "/page/family/family_list.jsp";
		String msg = "添加失败";
		String familyattribute=request.getParameter("familyattribute");
		String town=request.getParameter("town");
		String village=request.getParameter("village");
		String group=request.getParameter("group");
		String address=request.getParameter("address");
		String fullname=request.getParameter("fullname");
		String idcard=request.getParameter("idcard");
		String relative=request.getParameter("relative");
		String sex=request.getParameter("sex");
		String nation=request.getParameter("nation");
		String health=request.getParameter("health");
		String culture=request.getParameter("culture");
		String attribute=request.getParameter("attribute");
		String farmer=request.getParameter("farmer");
		String marry=request.getParameter("marry");
		String workunit=request.getParameter("workunit");
		String tel=request.getParameter("tel");

		User user =new User();
		user=(User)request.getSession().getAttribute("user");
		Family f= new Family(familyattribute,town, village, group, address, user.getUserid());
		Person p = new Person(fullname, idcard, relative, sex, nation, health, culture, attribute, farmer, marry, workunit, tel);
		f.setHolder(p);
		f.add();
		jump(request,response,path);
	}


	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/family/family_list.jsp";
		String familycode = request.getParameter("familycode");
		familycode = new String(familycode.getBytes("ISO-8859-1"),"UTF-8");
		Family f = new Family();
		f.del(familycode);
		jump(request,response,path);
	}
	
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/family/family_list.jsp";
		String msg = "修改失败";
		String familycode=request.getParameter("familycode");
		String familyattribute=request.getParameter("familyattribute");
		String town=request.getParameter("town");
		String village=request.getParameter("village");
		String group=request.getParameter("group");
		String address=request.getParameter("address");
		
		Family f= new Family();
		f.setFamilycode(familycode);
		f.setFamilyattribute(familyattribute);
		f.setTown(town);
		f.setVillage(village);
		f.setGroup(group);
		f.setAddress(address);
		f.edit();
		jump(request,response,path);
	}
	

}
